package com.qk.controller;

import com.qk.common.Result;
import com.qk.common.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@Slf4j
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile  file) throws Exception {
//        log.info("{},{},{}",username,age,file);
//        //构造一个文件名
//        String filename =  file.getOriginalFilename();//获取原始文件名
//        //截取后缀
//        String suffix = filename.substring(filename.lastIndexOf("."));
//        //使用UUID生成文件名
//        String newfilename = UUID.randomUUID().toString() + suffix;
//        //将文件保存到磁盘中
//        file.transferTo(new File("D:\\images\\"+newfilename));
//        return Result.success();
//    }
    //文件上传
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("文件上传:{}",image);
        //构造一个文件名
        String filename = image.getOriginalFilename();//获取原始文件名
        //截取后缀
        String suffix = filename.substring(filename.lastIndexOf("."));
        //使用UUID生成文件名
        String newfilename = UUID.randomUUID().toString() + suffix;
        String url = aliyunOSSOperator.upload(image.getBytes(), newfilename);
        //上传到阿里云

        return Result.success(url);
    }
}
